// miniprogram/pages/order/detail.js
import * as R from 'ramda';
import U from '../../utils/index';
import config from '../../config/index'

const requestDetail = data => U.handleRequest({}, () => U.reqPost({
  url:'/order/get_list',
  data
}))
const requestTask = data => U.handleRequest({}, () => U.reqPost({
  url:'/task/task_list',
  data
}))
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    orderId: null,
    isShowDetail: false,
    pageData: null,
    isShowTask: false,
    taskData: null,
    taskTypeNames: config.taskTypeNames,
    orderTypes: config.orderTypes,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    R.compose(
      this.loadData,
      // 数据为空就会重新加载，所以清空以便onShow时都能加载数据
      () => this.setData({
        pageData: null,
        taskData: null
      }),
    )()
  },
  loadData() {
    R.cond([
      [
        () => this.data.index === 1 && R.isNil(this.data.taskData),
        R.compose(
          U.fork,
          R.map(res => {
            this.setData({
              taskData: R.reverse(res.data.list),
              isShowTask: true,
            })
          }),
          () => requestTask({ order_id: this.data.orderId })
        )
      ],
      [
        () => this.data.index === 0 && R.isNil(this.data.pageData),
        R.compose(
          U.fork,
          R.map(res => {
            this.setData({
              pageData: R.clone(res.data.list[0]),
              isShowDetail: true,
            })
          }),
          () => requestDetail({ order_id: this.data.orderId })
        )
      ]
    ])()
  },
  onClickTabs(e) {
    R.compose(
      this.loadData,
      () => this.setData({ index: e.detail.index })
    )()
  },
})
